import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入全局弹框信息
import {
	Message
} from 'element-ui';
// import HomeView from '../views/HomeView.vue'
Vue.use(VueRouter)
const routes = [{
		path: '/',
		name: 'login',
		component: () => import("../views/user/LoginView.vue")
	},
	{
		path: '/admin',
		name: 'admin',
		component: () => import("../views/admin/AdminView.vue"),
		meta: {
			requireAuth: true,
		},
		// 定义自路由
		children: [
			{name: "dash",
				path: "dash",
				component: () => import("../views/admin/DashView.vue")
			},
			{
				name: "orderlist",
				path: "orderlist",
				component: () => import("../views/admin/order/OrderList.vue")
			},
			{
				name: "orderdetail",
				path: "orderdetail/:OrderNum",
				component: () => import("../views/admin/order/OrderDetail.vue")
			},
			// 如果没有自定子路由，跳转大dash页面
			{
				path: "",
				redirect: 'dash'
			}
		]
	},
	{
		path: '/about',
		name: 'about',
		// route level code-splitting
		// this generates a separate chunk (about.[hash].js) for this route
		// which is lazy-loaded when the route is visited.
		component: () => import( /* webpackChunkName: "about" */ '../views/AboutView.vue')
	}
]

const router = new VueRouter({
	routes
})
// 路由权限信息
router.beforeEach((to, from, next) => {
	// 如果需要权限
	if (to.meta.requireAuth) {
		// 是否有token
		if (localStorage.getItem("token")) {
			// 有个token放行
			next();
		} else {
			// 如果没有跳转到首页，查询参数redirect是当前页的path
			Message({
				message: '需要先登录！',
				type: 'warning'
			});
			next("/?redirect=" + to.path)
		}
	} else {
		// 如果没有权限那么就直接放行
		next();
	}
})
export default router
